/**************************************************************************************
* @file   : CAN_Heartbeat_Check_Task.c
* @version: 
* @date   : 2024-09-04
* @file   : George
* @brief  : 
**************************************************************************************/
#include "CAN_Heartbeat_Check_Task.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "can_bsp.h"
#include "safe_task.h"
#include "start_task.h"
#include "custom_lib.h"
#include "relay_bsp.h"
#include "start_task.h"

extern osThreadId_t CAN_Heartbeat_Check_TaskHandle;
extern osTimerId_t CAN_Heartbeat_Check_TimerHandle;

extern __IO uint8_t motor1_can_heartbeat_flag;
extern __IO uint8_t motor2_can_heartbeat_flag;
extern __IO uint8_t motor3_can_heartbeat_flag;
extern __IO uint8_t motor4_can_heartbeat_flag;
extern __IO uint8_t motor5_can_heartbeat_flag;
extern __IO uint8_t motor6_can_heartbeat_flag;
extern __IO uint8_t motor7_can_heartbeat_flag;
extern __IO uint8_t motor8_can_heartbeat_flag;

__IO uint8_t motor1_can_heartbeat_ok_flag = 0;
__IO uint8_t motor2_can_heartbeat_ok_flag = 0;
__IO uint8_t motor3_can_heartbeat_ok_flag = 0;
__IO uint8_t motor4_can_heartbeat_ok_flag = 0;
__IO uint8_t steer1_can_heartbeat_ok_flag = 0;
__IO uint8_t steer2_can_heartbeat_ok_flag = 0;
__IO uint8_t steer3_can_heartbeat_ok_flag = 0;
__IO uint8_t steer4_can_heartbeat_ok_flag = 0;

void CAN_Heartbeat_Check_Callback(void *argument)
{
  /* USER CODE BEGIN CAN_Heartbeat_Check_Callback */
  if(1 == motor1_can_heartbeat_flag)
  {
    motor1_can_heartbeat_flag = 0;
    motor1_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor1 heartbeat ok!\r\n");    
  }
  else
  {
    motor1_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor1 heartbeat error!\r\n");
  }
  
  if (1 == motor2_can_heartbeat_flag)
  {
    motor2_can_heartbeat_flag = 0;
    motor2_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor2 heartbeat ok!\r\n");    
  }
  else 
  {
    motor2_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor2 heartbeat error!\r\n");
  }
  
  if (1 == motor3_can_heartbeat_flag)
  {
    motor3_can_heartbeat_flag = 0;
    motor3_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor3 heartbeat ok!\r\n");    
  }
  else
  {
    motor3_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor3 heartbeat error!\r\n");
  }
  
  if (1 == motor4_can_heartbeat_flag)
  {
    motor4_can_heartbeat_flag = 0;
    motor4_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor4 heartbeat ok!\r\n");    
  }
  else
  {
    motor4_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor4 heartbeat error!\r\n");
  }
  
  if (1 == motor5_can_heartbeat_flag)
  {
    motor5_can_heartbeat_flag = 0;
    steer1_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor5 heartbeat ok!\r\n");    
  }
  else
  {
    steer1_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor5 heartbeat error!\r\n");
  }
  
  if (1 == motor6_can_heartbeat_flag)
  {
    motor6_can_heartbeat_flag = 0;
    steer2_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor6 heartbeat ok!\r\n");    
  }
  else
  {
    steer2_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor6 heartbeat error!\r\n");
  }
  
  if (1 == motor7_can_heartbeat_flag)
  {
    motor7_can_heartbeat_flag = 0;
    steer3_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor7 heartbeat ok!\r\n");    
  }
  else
  {
    steer3_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor7 heartbeat error!\r\n");
  }
  
  if (1 == motor8_can_heartbeat_flag)
  {
    motor8_can_heartbeat_flag = 0;
    steer4_can_heartbeat_ok_flag = 1;
//    LOG_INFO(1,"motor8 heartbeat ok!\r\n");    
  }
  else
  {
    steer4_can_heartbeat_ok_flag = 0;
//    LOG_INFO(1,"motor8 heartbeat error!\r\n\n");
  }
  /* USER CODE END CAN_Heartbeat_Check_Callback */
}
/**************************************************************************************
** @name:              CAN_Heartbeat_Check
** @brief:             
** @param:             
** @retval:            
** @author:            George
** @date:              2024-09-04
** @version:           V0.0
**************************************************************************************/
void CAN_Heartbeat_Check_APP(void const * argument)
{
    /* USER CODE BEGIN KeyTask_Entry */
    /* Infinite loop */
    uint32_t tick;
    tick = osKernelGetTickCount();
    osTimerStart(CAN_Heartbeat_Check_TimerHandle, 1200U);
    for(;;)
    {
        osThreadTerminate(CAN_Heartbeat_Check_TaskHandle);

//        //等待事件标志
//        can_heartbeat_event_return = osEventFlagsWait(CAN_HeartbeatHandle,
//        motor1_can_heartbeat|motor2_can_heartbeat|motor3_can_heartbeat|motor4_can_heartbeat|
//        steer1_can_heartbeat|steer2_can_heartbeat|steer3_can_heartbeat|steer4_can_heartbeat,
//        osFlagsWaitAny,1200);

//        if((can_heartbeat_event_return & (motor1_can_heartbeat)) == motor1_can_heartbeat)//驱动器报错
//        { 
//            //do something..
//            osEventFlagsClear(CAN_HeartbeatHandle,motor1_can_heartbeat);//清除事件标志
//        }
//        else
//        {
////            LOG_INFO(0,"motor1 heartbeat error!\r\n");
//        }
    }
    /* USER CODE END KeyTask_Entry */
}
